package android.crosstime.hes;

import java.io.File;

import edu.android.hes.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class FloatDialog extends RelativeLayout {
	
	private LayoutInflater mInflater = null;
	private RelativeLayout mFloatView = null;
	private ImageView mCloseBtn = null;
	private ImageView mMiniBtn = null;
	private ImageView mDragBtn = null;
	private ListView mFloatList = null;
	private TextView mTitleText = null;
	private View mMaskView = null;
	private File mCurrentDir = null;
	private Context mContext = null;
	BaseFileListAdapter mListAdapter = null;
//	private final static int MODE_STATIC = 0;
//	private final static int MODE_DYNAMIC = 1;
//	
//	private int mMode = MODE_STATIC;
	
	private int m_fLastX, m_fLastY;
	
	public FloatDialog(Context context, String dirPath) {
		super(context);
		mContext = context;
		mInflater = LayoutInflater.from(context);
		mCurrentDir = new File(dirPath);
		initViews();
	}
	
	public FloatDialog(Context context, AttributeSet attrs) {
		super(context, attrs);
		mInflater = LayoutInflater.from(context);
	}

	
	private void initViews(){
		mFloatView = (RelativeLayout) mInflater.inflate(R.layout.float_dialog, null);
		mCloseBtn = (ImageView) mFloatView.findViewById(R.id.close_img);
		mMiniBtn = (ImageView) mFloatView.findViewById(R.id.minimize_img);
		mDragBtn = (ImageView) mFloatView.findViewById(R.id.drag_img);
		
		mFloatList = (ListView)mFloatView.findViewById(R.id.float_listview);
		mTitleText = (TextView)mFloatView.findViewById(R.id.title_text);
		mTitleText.setText(mCurrentDir.getAbsolutePath());
		mCloseBtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {

				FloatDialog.this.detachAllViewsFromParent();
				invalidate();
			}});
		mMiniBtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				((HExplorer)mContext).addToContainer(FloatDialog.this);
				FloatDialog.this.detachAllViewsFromParent();
				invalidate();
			}});
		mDragBtn.setOnTouchListener(mTitleTouchListener);
		mListAdapter = new BaseFileListAdapter(mContext);
		mListAdapter.changeDirectory(mCurrentDir.getAbsolutePath());
		mFloatList.setAdapter(mListAdapter);
		mFloatList.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				String newPath = mListAdapter.getFilePathByPosition(position);
				if(mListAdapter.changeDirectory(newPath))
				{
					mFloatList.setAdapter(mListAdapter);
					mTitleText.setText(newPath);
					mCurrentDir = new File(newPath);
				}
			}});
		mMaskView = mFloatView.findViewById(R.id.mask_view);
		mMaskView.setOnTouchListener(new OnTouchListener(){

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				FloatDialog.this.bringToFront(); //put the touched dialog in front
		        postInvalidate();
				return false;
			}});
		this.addView(mFloatView);
	}

	private int offset_X = 0, offset_Y = 0;
	private OnTouchListener mTitleTouchListener = new OnTouchListener(){

		@Override
		public boolean onTouch(View v, MotionEvent event) {

			int action = event.getAction();
			switch (action) {
			
			case MotionEvent.ACTION_DOWN:
				m_fLastX = (int) event.getRawX();
		        m_fLastY = (int) event.getRawY();
		        offset_X = FloatDialog.this.getLeft() - m_fLastX;
		        offset_Y = FloatDialog.this.getTop() - m_fLastY;
//		        FloatDialog.this.bringToFront(); //put the touched dialog in front
//		        postInvalidate();
			    break;
				
			case MotionEvent.ACTION_MOVE:
		        int left = (int) event.getRawX() ;
		        int top = (int)event.getRawY();
		        int right = left + FloatDialog.this.getMeasuredWidth();
		        int bottom = top + FloatDialog.this.getMeasuredHeight();

				if(m_fLastX - 10 < left && left < m_fLastX + 10 
						&& m_fLastY - 10 < top && top < m_fLastY + 10)
					break;

		        FloatDialog.this.layout(left+offset_X, top+offset_Y, right+offset_X, bottom+offset_Y);

		        m_fLastX = left;
		        m_fLastY = top;
				break;
				
			case MotionEvent.ACTION_UP:
				postInvalidate();
				
				//Update layout for this view
				RelativeLayout.LayoutParams newLayout = (LayoutParams) FloatDialog.this.getLayoutParams();
				newLayout.leftMargin = m_fLastX + offset_X;
				newLayout.topMargin = m_fLastY + offset_Y;
				((HExplorer)mContext).getDialogLayout().updateViewLayout(FloatDialog.this, newLayout);
				
				if(((HExplorer)mContext).checkIfInContainer(m_fLastX, m_fLastY, FloatDialog.this))
				{
					FloatDialog.this.detachAllViewsFromParent();
					invalidate();
				}
				
				break;
			default:
				break;
			}
			return false;
		}};
		
	public File getCurrentDir(){ 
		return mCurrentDir;
	}
}
